package com.maya.medicalwastes.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.maya.medicalwastes.R;
import com.maya.medicalwastes.util.DeviceUtil;
import com.maya.medicalwastes.util.ToastUtils;


/**
 * *
 */
public class UploadErrorDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private View view;
    private int heightRatio = 15;
    private int widthRatio = 7;
    private Button btnCancel;
    private Button btnOk;
    private TextView tv_wate_title;
    private String name;
    private boolean isCancelable = false;
    String type = "";
    private CheckBox checkbox1, checkbox2, checkbox3;
    private EditText et_content;

    public UploadErrorDialog(@NonNull Context context, String name) {
        super(context, R.style.custom_dialog);
        this.context = context;
        this.name = name;
        initView();
        initDate();
    }

    private void initDate() {

    }

    private void initView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.dialog_upload_error, null);
        setContentView(view);

        btnCancel = findViewById(R.id.btn_cancel);
        et_content = findViewById(R.id.et_content);
        btnOk = findViewById(R.id.btn_ok);
        tv_wate_title = findViewById(R.id.tv_wate_title);
        checkbox1 = findViewById(R.id.checkbox1);
        checkbox2 = findViewById(R.id.checkbox2);
        checkbox3 = findViewById(R.id.checkbox3);
        tv_wate_title.setText(name);
        btnOk.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_close) {
            dismiss();
        }

        if (v.getId() == R.id.btn_ok) {
            if (onBtnClickListener != null) {
                if (!et_content.getText().toString().equals("")) {
                    onBtnClickListener.onOkClick(type, et_content.getText().toString());
                } else {
                    ToastUtils.ViewToast(context, "请完善信息");
                }
            }
        }

        if (v.getId() == R.id.btn_cancel) {
            if (onBtnClickListener != null) {
                onBtnClickListener.onCancelClick();
            }
            dismiss();
        }

        if (v.getId() == R.id.checkbox1) {
            type = checkbox1.getText().toString();
        }
        if (v.getId() == R.id.checkbox2) {
            type = checkbox2.getText().toString();
        }
        if (v.getId() == R.id.checkbox3) {
            type = checkbox3.getText().toString();
        }
    }

    public void popDialog() {
        setCancelable(isCancelable);
//        backgroundAlpha(0.6f);
        show();
        Window window = getWindow();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
        p.gravity = Gravity.CENTER;
//        p.height = (int) (DeviceUtil.getDisplayMetrics(context).heightPixels * 7 / 13);
        p.width = DeviceUtil.getDisplayMetrics(context).widthPixels * 6 / 13;
        window.setAttributes(p);
    }

    public OnBtnClickListener onBtnClickListener;

    public OnBtnClickListener getOnBtnClickListener() {
        return onBtnClickListener;
    }

    public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
        this.onBtnClickListener = onBtnClickListener;
    }

    public interface OnBtnClickListener {
        void onOkClick(String type, String string);

        void onCancelClick();
    }

    public void setHeightRatio(int heightRatio) {
        this.heightRatio = heightRatio;
    }

    public void setWidthRatio(int widthRatio) {
        this.widthRatio = widthRatio;
    }

    public void setSizeRatio(int widthRatio, int heightRatio) {
        this.widthRatio = widthRatio;
        this.heightRatio = heightRatio;
    }

    @Override
    public void setCancelable(boolean cancelable) {
        isCancelable = cancelable;
    }
}
